// pages/productOrder/productOrder.js
const app = getApp()
const request = require("../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: 0,
    orderBarIndex: 0,
    projectList: [],
    verificationDialog: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      screenHeight: wx.getSystemInfoSync().screenHeight
    })
    this.getProjectList()
  },
  /**
   * 切换tab
   */
  selectTab(e) {
    let index = e.currentTarget.dataset.index
    if (index == this.data.orderBarIndex) return
    this.setData({
      orderBarIndex: index
    })
    this.getProjectList()
  },
  /**
   * 获取订单列表
   */
  getProjectList() {
    let that = this
    let parmas = {
      status: parseInt(this.data.orderBarIndex) + 1
    }
    request.http('/shop/shop-order/getMyOrderInfoListByStatus', 'GET', parmas)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          that.setData({
            projectList: data.data
          })
        }
      })
  },
  /**
   * 退款
   */
  refund(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let projectList = this.data.projectList
    wx.showModal({
      content: '是否确认退款',
      success(res) {
        if (res.confirm) {
          request.http('/shop/shop-order/refundGoods/' + projectList[index].shopOrderId, 'GET')
            .then(res => {
              let data = res.data
              if (data.code == 200) {
                wx.showToast({
                  title: '退款完成',
                  success() {
                    that.getProjectList()
                  }
                })
              }
            })
        }
      }
    })
  },
  /**
   * 去使用
   */
  toUse(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let projectList = this.data.projectList
    request.http('/shop/shop-order/useGoods/' + projectList[index].shopOrderId, 'GET')
      .then(res => {
        let data = res.data
        that.setData({
          verificationDialog: true,
          verificationInfo: {
            code: projectList[index].writeOffCode,
            name: projectList[index].goodsName,
            image: data
          }
        })
      })
  },
  /**
   * 关闭核销弹窗
   */
  hideVerificationDialog() {
    this.setData({
      verificationDialog: false
    })
  }
})